package com.xiaoye.qbproject.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoye.qbproject.domain.SubjectZl;
import com.xiaoye.qbproject.mapper.SubjectZlMapper;
import com.xiaoye.qbproject.service.SubjectZlService;
import com.xiaoye.qbproject.vo.SubjectZlVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【SUBJECT_ZL(装备与战例关系表)】的数据库操作Service实现
 * @createDate 2025-02-11 18:04:14
 */
@Service
public class SubjectZlServiceImpl extends ServiceImpl<SubjectZlMapper, SubjectZl>
        implements SubjectZlService {

    @Override
    public List<SubjectZlVo> getList(SubjectZlVo subjectZlVo) {
        return baseMapper.getList(subjectZlVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SubjectZlVo subjectZlVo) {
        List<String> oldZlIds = baseMapper.selectList(Wrappers.<SubjectZl>lambdaQuery()
                .eq(SubjectZl::getSubjectId, subjectZlVo.getSubjectId())
        ).stream().map(SubjectZl::getZlId).collect(Collectors.toList());

        subjectZlVo.getZlIds().stream().filter(zlId -> !oldZlIds.contains(zlId)).forEach(zlId -> {
            SubjectZl subjectZl = new SubjectZl();
            subjectZl.setSubjectId(subjectZlVo.getSubjectId());
            subjectZl.setZlId(zlId);
            baseMapper.insert(subjectZl);
        });
    }
}




